Repository navigation
[pm] fix race condition and lock protection in rt_pm_device_register/unregister - #12075
Conversation
…unregister 1. Initialize `device_pm` fields before appending the node to `_pm.device_list` in `rt_pm_device_register()` to prevent exposing uninitialized pointers. 2. Protect `_pm.device_list` modifications with `rt_hw_interrupt_disable()` / `rt_hw_interrupt_enable()` while keeping `RT_KERNEL_MALLOC()` and `RT_KERNEL_FREE()` outside the interrupt-disabled critical section to avoid SMP hang (RT-Thread#10075). Fixes RT-Thread#10075 Signed-off-by: niuxiaoch <niuxiaolong893@gmail.com>
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_driver_pmReviewers: @wdfk-prog Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-21 10:44 CST)
📝 Review Instructions
|
BernardXiong
left a comment
There was a problem hiding this comment.
Review / 审查 — 8fdb470f
结论:COMMENT — 锁的放置本身正确,但“修复竞态”仍不完整。
- 正确之处:
device_pm先初始化再挂链;RT_KERNEL_MALLOC/RT_KERNEL_FREE留在临界区外;unregister 先摘链、再在临界区外释放,避免带中断关闭执行阻塞操作。 - 【P2】
_pm_device_suspend()/_pm_device_resume()/_pm_device_frequency_change()仍无锁遍历_pm.device_list(pm.c:121、pm.c:153、pm.c:175)。unregister 在rt_hw_interrupt_enable()之后才释放节点(pm.c:882-885),若 suspend/resume 正持有已摘链的节点,仍存在 use-after-free 窗口。本 PR 只排除了 register/unregister 之间的链表破坏,未覆盖读者;建议读者侧使用同一把锁(或与 PM 调用上下文一致的 spinlock/mutex),至少应在接口注释中声明 register/unregister 不可与 suspend/resume 并发。 - 【P3】
rt_hw_interrupt_disable()在 SMP 上只关闭当前核中断,不能阻止其他核同时 register/unregister;若允许跨核调用,链表完整性仍未保证。若框架约定只在初始化阶段单线程注册,请在函数注释中写明。 - 已核对
RT_DEBUG_NOT_IN_INTERRUPT在非调试构建下为空实现,新增断言不会引入构建问题。
CI 为 success;未做动态验证。
Document that _pm.device_list updates are serialized with PM list walkers via rt_hw_interrupt_disable() (rt_cpus_lock() on SMP), and that the APIs must not be called from interrupt context or rt_device_pm_ops callbacks. Restore the original unregister comment wording. Signed-off-by: niuxiaoch <niuxiaolong893@gmail.com>
|
@BernardXiong 感谢审查!针对 P2/P3 我核对了当前 master 源码: P2:三个遍历函数都已在同一把锁内被调用——
P3:include/rthw.h:146 在 RT_USING_SMP 下将 rt_hw_interrupt_disable 定义为 rt_cpus_lock,而 rt_cpus_lock()(src/cpu_mp.c:168-196)会关本核中断、锁调度器并获取全局自旋锁 _cpus_lock,因此 SMP 下也能提供跨核互斥。 剩余风险是 BSP 覆盖 rt_weak 的 rt_pm_enter_critical() 且互斥强度更弱,或在 rt_device_pm_ops 回调中调用 unregister。我已追加提交,在接口注释中写明上述加锁约定和调用限制,同时恢复了原有的 unregister 注释措辞。请再看一下,谢谢! |
BernardXiong
left a comment
There was a problem hiding this comment.
Review — 199b25c8
结论:APPROVE(增量仅注释,未改行为)。
本轮增量只新增/整理接口注释(pm.c:835-878)。重新核对后,我上一轮 P2 的前提不成立,特此更正:
_pm_device_suspend()/_pm_device_resume()只在_pm_change_sleep_mode()的rt_pm_enter_critical()…rt_pm_exit_critical()区间内遍历(pm.c:395、pm.c:425、pm.c:442、pm.c:476)。_pm_device_frequency_change()在rt_hw_interrupt_disable()内遍历(pm.c:1008-1017)。rt_pm_enter_critical()默认实现即rt_hw_interrupt_disable()(pm.c:72-75);SMP 下该宏映射为rt_cpus_lock(rthw.h:146),具备跨核互斥。
因此 unregister 解锁后再释放节点的窗口不成立;新增注释与实现一致,仅保留“BSP 覆盖 rt_pm_enter_critical() 时必须提供同等互斥”的约束,且已在注释中声明。CI 全绿;未做动态验证。
|
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
Fixes #10075
In
components/drivers/pm/pm.c:rt_pm_device_register()previously appendeddevice_pm->listto_pm.device_listbefore initializingdevice_pm->deviceanddevice_pm->ops, and without interrupt disable protection.rt_pm_device_unregister()traversed and modified_pm.device_listwithout interrupt disable protection and calledRT_KERNEL_FREE()directly inside the loop.你的解决方案是什么 (what is your solution)
device_pmfields and list node before appending to_pm.device_listinrt_pm_device_register()._pm.device_listoperations withrt_hw_interrupt_disable()/rt_hw_interrupt_enable()while keepingRT_KERNEL_MALLOC()andRT_KERNEL_FREE()outside the interrupt-disabled critical section to avoid SMP hang ([Bug]components/drivers/pm/pm.c rt_pm_device_register API would be hang when RT_USING_SMP is configured #10075).请提供验证的bsp和config (provide the config and bsp)
BSP: bsp/qemu-vexpress-a9
.config: CONFIG_RT_USING_PM=y
action: CI build check
]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up